package stark.app.function.word;

import android.Manifest;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.word.lib.model.WordUtil;

import java.util.List;

import androidx.annotation.NonNull;
import stark.app.R;
import stark.app.databinding.ActivityWordDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.IntentUtil;

/**
 * Author：daizhongbin on 2021/12/14
 * Email：1027154338@qq.com
 * Desc:
 */
public class WordDemoActivity extends BaseNoModelActivity<ActivityWordDemoBinding> {

    @Override
    protected int onCreate() {
        return R.layout.activity_word_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnSave2Word.setOnClickListener(v -> {
            clickSave2Word();
        });
    }

    @Override
    protected void initData() {

    }

    private void createWord() {
        showDialog("处理中...");
        String content = "爱是什么？\n" +
                "一个精灵坐在碧绿的枝叶间沉思。\n" +
                "风儿若有若无。\n" +
                "一只鸟儿飞过来，停在枝上，望着远处将要成熟的稻田。\n" +
                "精灵取出一束黄澄澄的稻谷问道：“你爱这稻谷吗？”";
        WordUtil.createWordDoc(content, new IRetCallback<String>() {
            @Override
            public void onResult(String result) {
                IntentUtil.shareFile(WordDemoActivity.this, result);
                dismissDialog();
            }
        });
    }

    private void clickSave2Word() {
        PermissionUtils.permission(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .callback(new PermissionUtils.SingleCallback() {
                    @Override
                    public void callback(boolean isAllGranted, @NonNull List<String> granted, @NonNull List<String> deniedForever, @NonNull List<String> denied) {
                        if (!isAllGranted) {
                            ToastUtils.showShort("未授予权限");
                            return;
                        }
                        createWord();
                    }
                })
                .request();
    }
}
